package com.bjSoft.regressionTestTool.TestSelection.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPart;

import com.bjSoft.regressionTestTool.TestSelection.RegressionTestSelect;
import com.bjSoft.regressionTestTool.TestSelection.TestSelectionException;
import com.bjSoft.regressionTestTool.TestSelection.ui.RegressionTestSelectionView;
import com.bjSoft.regressionTestTool.Activator;

public class RunSelectionActionDelegate implements IObjectActionDelegate {

	private ISelection selection;
	private IProject project;
	
	public RunSelectionActionDelegate() {
		// TODO Auto-generated constructor stub
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		
	}

	public void run(IAction action) {
		if (selection instanceof IStructuredSelection) {
			for (Iterator it = ((IStructuredSelection) selection).iterator(); it
					.hasNext();) {
				Object element = it.next();
				IProject project = null;
				if (element instanceof IProject) {
					project = (IProject) element;
				} else if (element instanceof IAdaptable) {
					project = (IProject) ((IAdaptable) element)
							.getAdapter(IProject.class);
				}
				if (project != null) {
					this.project = project;
				}
			}
		}		
		if (this.project != null) {
			RegressionTestSelect rts = new RegressionTestSelect();
			try {
				HashMap<String, List<String>> result= 
										rts.selectTestCases(project);
				Activator.getDefault().getWorkbench().
						getActiveWorkbenchWindow().
								getActivePage().showView(RegressionTestSelectionView.ID);
				IViewReference[] viewReferencies = 
					Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
					.getActivePage().getViewReferences();
				for (int i = 0; i < viewReferencies.length; i++) {
					if (viewReferencies[i].getId()
						.equalsIgnoreCase(RegressionTestSelectionView.ID)) {
						RegressionTestSelectionView rTSView = 
							(RegressionTestSelectionView) viewReferencies[i].getView(true);
						rTSView.setData(result, project);					
					}
				}
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TestSelectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;	
		
	}

}
